package com.zjy.service.impl;

import com.zjy.bean.User;
import com.zjy.dto.UserInfo;
import com.zjy.dao.UserMapper;
import com.zjy.service.RegisterService;
import com.zjy.utils.TokenUtils;
import com.zjy.utils.UUIDUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Created by Administrator on 2019/2/27.
 */
@Service
public class RegisterServiceImpl implements RegisterService {
    @Resource
    private UserMapper userMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserInfo register(User user){
        //生成一个uuid作为主键id
        String id = UUIDUtils.getUUID();
        String userName = user.getUserName();
        user.setId(id);
        userMapper.insertSelective(user);
        String token = TokenUtils.createTokenByUserId(id);  //利用userId生成一个token返回给客户端浏览器
        UserInfo userInfo = new UserInfo(id,userName,token);    //创建一个包含userId和token的实体
        return userInfo;
    }

}
